package ar.org.cfired.creditos.gui;

import org.jbpm.api.Configuration;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.TaskService;
import org.jbpm.api.IdentityService;
import org.jbpm.api.identity.*;
import org.jbpm.api.task.*;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Set;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Date;


import java.util.Iterator;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

import org.jbpm.api.ExecutionService;
import org.jbpm.api.ProcessInstanceQuery;
import org.jbpm.api.ProcessInstance;

public class PruebaAPI {
  public static void main (String args[]) {
	 
	 String usuario;
	 usuario = "fbarletta";
	 ProcessEngine processEngine = new Configuration().buildProcessEngine();
	 TaskService taskService = processEngine.getTaskService();
	 //ExecutionService executionService = processEngine.getExecutionService();

	 ExecutionService ex = processEngine.getExecutionService();

	 
	 Task tar = taskService.getTask("8596");
	 System.out.println(Encabezado.toHTML(tar, "E", taskService, tar.getId()));
	 
	 System.exit(0);
	 /*	 ProcessInstanceQuery q = ex.createProcessInstanceQuery();
	 q = q.notSuspended();
	 //q = q.orderAsc(org.jbpm.api.history.HistoryActivityInstanceQuery.PROPERTY_ACTIVITYNAME);
	 //q = q.state("Solicitudes Recibidas");
	 List<ProcessInstance> lista = (List<ProcessInstance>)q.list();	 
	 System.out.println(lista.size());
	 Iterator<ProcessInstance> i = lista.iterator();
	 while (i.hasNext()) {
		 System.out.println(((ProcessInstance)i.next()).toString());
	 }
	 System.exit(0);
	*/ 
	 
	 List<Task> tareasGrupales = taskService.findGroupTasks(usuario);
	 List<Task> tareasPersonales = taskService.findPersonalTasks(usuario);

	 Iterator<Task> iter;
	 Task tarea = null;
	 iter = (Iterator<Task>) tareasGrupales.iterator();
	 System.out.println("Grupales");
	 

	 while (iter.hasNext()) {
		 tarea = iter.next();
		 System.out.println(tarea.getExecutionId());
		 Tarea tareaPropia = new Tarea(processEngine, false, tarea.getId());
		 tareaPropia.addCampo("cre_id","20px");
		 tareaPropia.addCampo("titNacionalidad","20px");
		 tareaPropia.addCampo("titCalle","20px");
		 System.out.println(tareaPropia.toHtml());
	 }
	 System.out.println("Personales");
	 iter = (Iterator<Task>) tareasPersonales.iterator();	 
	 while (iter.hasNext()) {
		 tarea = iter.next();
		 System.out.println(tarea.getExecutionId());
		 Tarea tareaPropia = new Tarea(processEngine, true, tarea.getId());
		 tareaPropia.addCampo("cre_id","20px");
		 tareaPropia.addCampo("titNacionalidad","20px");
		 tareaPropia.addCampo("titCalle","20px");
		 System.out.println(tareaPropia.toHtml());		 
	 }
	 //Busco el usuario
	 //IdentityService identityService = processEngine.getIdentityService();
	 //User user = identityService.findUserById(usuario);
	 //System.out.println(user.getFamilyName());
	 
	// System.exit(0);
	  String salida = "";
	  String fin = "";
	  String str = "";
	  
	  
	 try {
	  BufferedReader entrada = new BufferedReader(new FileReader("c:/workspace/Creditos/src/ar/org/cfired/creditos/ActuacionesViables.ftl"));
	   char[] buf = new char[1024];
       int numRead=0;
       while((numRead=entrada.read(buf)) != -1){
           str += String.valueOf(buf, 0, numRead);
           buf = new char[1024];
       }


//	  System.out.println(str);
       System.out.println("Tiempo inicial" + (new Date()));
	  //String str = "hola ${aasdfgsf}, ${bbzzz}{}}{{$$$$}}{}";
		Pattern p = Pattern.compile("\\$\\{[^\\}]*\\}");
		Matcher m = p.matcher(str);
		//boolean b = m.matches();
	int pos = 0;
	
	
	Set<String> variables = new HashSet<String>();
	System.out.println("Tiempo Antes de expr" + (new Date()));	
	String variable;
		while( m.find()) {
			variable = str.substring(m.start()+2, m.end()-1);
			variables.add(variable);
		}
		salida = str;
		System.out.println("Tiempo Luego de expr" + (new Date()));	
		
		Map<String,Object> respuestas = taskService.getVariables(tarea.getId(), variables);
/*		Iterator<String> itera = variables.iterator();
		
		while (itera.hasNext()) {
			variable = itera.next();
			if (respuestas.containsKey(variable)) {
				salida = salida.replace("${" +  variable + "}", ((respuestas.get(variable)==null)?"":respuestas.get(variable).toString()));
			}
		}*/
		System.out.println("Tiempo Final" + (new Date()));

		//System.out.println(salida + fin);


		m = p.matcher(str);
		System.out.println ("Tiempo inicio Prueba 2" + (new Date()));
		pos = 0;
		salida = "";
		while( m.find()) {
			variable = str.substring(m.start()+2, m.end()-1);
			salida += str.substring(pos, m.start());
			salida += respuestas.get(variable);			
			pos = m.end();
			//System.out.println(variable + " - " + respuestas.get(variable));
		}
		salida += str.substring(pos);
		System.out.println ("Tiempo fin Prueba 2" + (new Date()));
		//System.out.println(salida + fin);
System.out.println(tarea.getId());
System.out.println(tarea.getFormResourceName());
	 }
	 catch(FileNotFoundException e) {System.out.println(e.toString());}
	 catch(IOException e) {System.out.println(e.toString());}
		
  }
}
